// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
import login from "@/pages/login";
import register from "@/pages/register";


import welcome from "@/components/welcome";
import data from "@/pages/mine/Mydata";
import updateData from "@/pages/mine/updateData";

import passages from "@/pages/passages";
import allPassages from "@/pages/passage/allPassages";
import passageDetail from "@/pages/passage/passageDetail";
import userAdmin from "@/pages/user/user-admin";
import dynamicAdmin from "@/pages/user/dynamic-admin";

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: welcome
        },
        {
            path: '/welcome',
            component: welcome
        },
        {
            path: '/login',
            component: login,
        },
        {
            path: '/register',
            component: register
        },
        {
            path: '/Mydata',
            component: data,
        },
        {
            path: '/updateData',
            component: updateData
        },
        {
            path: '/passages',
            component: passages,
            redirect: '/allPassages',
            children:[
                {
                    path: '/allPassages',
                    component: allPassages
                },
            ]
        },
        {
            path: '/passageDetail',
            component: passageDetail
        },
        {
            path:'/user-admin',
            component: userAdmin
        },
        {
            path:'/dynamic-admin',
            component: dynamicAdmin
        }

    ],
})

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    console.log('前置路由守卫', to, from)
    if (to.fullPath != "/login" && to.fullPath != "/register"&&to.fullPath!="/"&&to.fullPath!="/welcome") {
        if (!sessionStorage.getItem("login")||sessionStorage.getItem("login")==''||sessionStorage.getItem("login")==undefined) {
            alert("请重新登录")
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
